<!--
 * @Author: gy
 * @Date: 2020-01-04 15:40:06
 * @LastEditors  : gy
 * @LastEditTime : 2020-06-28 22:44:36
 -->
<template>
  <div class="table-wrapper">
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      :show-summary="showSummary"
      :default-sort="defaultSort"
      @filter-change="filterChange"
      @header-click="headerClick"
      @row-contextmenu="rowContextmenu"
      @row-click="rowClick"
      @select="selectClick"
      @selection-change="selectionChange"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      :row-key="rowKeys"
      :default-expand-all="expandAll"
    >
      <slot />
    </el-table>
    <el-pagination
      :current-page="1"
      :page-sizes="[15, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      :hide-on-single-page="true"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
      @prev-click="handlePageChange"
      @next-click="handlePageChange"
    />
  </div>
</template>

<script>
const int = null
import Vue from 'vue'
export default {
  components: {},
  props: {
    data: {
      type: Array,
      default: () => {
        return []
      }
    },
    totalCount: {
      type: Number,
      default: () => {
        return 0
      }
    },
    loading: {
      type: Boolean,
      default: false
    },
    staticTable: {
      type: Boolean,
      default: false
    },
    pageSize: {
      type: Number,
      default: 15
    },
    showSummary: {
      type: Boolean,
      default: false
    },
    rowKeys: {
      type: String,
      default: () => ""
    },
    expandAll: {
      type: Boolean,
      default: () => true
    }
  },
  data() {
    return {
      columnMap: {},
      curCol: {}, // 当前点击行
      filters: [],
      currentPage: 1,
      hasFilters: false,
      tableData: this.data,
      poper: null,
      defaultSort: { prop: 'createTime', order: 'descending' },
      popperShow: false,
      reference: null,
      filterEle: null,
      dataTypes: ''
    }
  },
  watch: {
    rowKeys: {
      handler(val) {
        console.log(val, 'rowKeys')
      },
      immediate: true
    },
    filters: {
      handler(val) {
        this.hasFilters = val.length != 0
        this.$emit('filter-change', JSON.parse(JSON.stringify(this.filters)))
      },
      deep: true
    },
    data(val) {
      if (this.staticTable) {
        this.tableData = this.data.slice(0, this.pageSize)
      } else {
        this.tableData = val
      }
    }
  },
  methods: {
    selectionChange(selection) {
      this.$emit('selectionChange',selection)
      // if (this.$getType(this.$listeners['selection-change']) == 'function') {
      //   this.$listeners['selection-change'].apply(this, [...arguments])
      // }
    },
    filterChange() {
      if (this.$getType(this.$listeners['filter-change']) == 'function') {
        this.$listeners['filter-change'].apply(this, [...arguments])
      }
    },
    headerClick() {
      if (this.$getType(this.$listeners['header-click']) == 'function') {
        this.$listeners['header-click'].apply(this, [...arguments])
      }
    },
    rowClick() {
      if (this.$getType(this.$listeners['row-click']) == 'function') {
        this.$listeners['row-click'].apply(this, [...arguments])
      }
    },
    selectClick() {
      if (this.$getType(this.$listeners['select']) == 'function') {
        this.$listeners['select'].apply(this, [...arguments])
      }
    },

    rowContextmenu() {
      if (this.$getType(this.$listeners['row-contextmenu']) == 'function') {
        this.$listeners['row-contextmenu'].apply(this, [...arguments])
      }
    },
    handleSizeChange(num) {
      this.pageSize = num
      if (this.staticTable) {
        const start = (this.currentPage - 1) * this.pageSize
        this.staticTable = this.data.slice(start, start + this.pageSize)
      } else {
        this.$emit('iPagination', {
          pageSize: this.pageSize,
          currentPage: this.currentPage
        })
      }
    },
    handlePageChange(page) {
      this.currentPage = page
      if (this.staticTable) {
        const start = (this.currentPage - 1) * this.pageSize
        this.tableData = this.data.slice(start, start + this.pageSize)
      } else {
        this.$emit('iPagination', {
          pageSize: this.pageSize,
          currentPage: this.currentPage
        })
      }
    }
  }
}
</script>

<style lang="scss">
.table-wrapper {
  .el-table {
    background-color: transparent;
  }
  //上border
  .el-table th.is-leaf,
  .el-table td {
    border-bottom: 0;
  }

  //下border
  .el-table::before,
  .el-table--group::after,
  .el-table--border::after {
    background-color: transparent;
  }

  .el-checkbox__inner {
    border-radius: 50%;
  }
  .el-table__header-wrapper {
    table {
      tr {
        background-color: transparent;
        th {
          background-color: transparent;

          &:first-child {
            //跟tbody checkbox对齐
            border-left: 6px solid transparent;
          }
        }
      }
    }
  }
  .el-table__body-wrapper {
    table {
      border-spacing: 0 10px;
    }
    tbody {
      tr {
        box-shadow: -1px 4px 6px 0px rgba(221, 222, 223, 0.9);
        border-radius: 4px;
        margin-bottom: 10px;
        &:nth-of-type(odd) {
          td {
            &:first-child {
              border-left: 2px solid #f18300;
              border-radius: 4px;
            }
          }
        }

        &:nth-of-type(even) {
          td {
            &:first-child {
              border-left: 2px solid #3399ff;
              border-radius: 4px;
            }
          }
        }
      }
    }
  }
}
</style>
